package com.liang.handler;

import cn.hutool.json.JSONUtil;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: XIAOLIANG
 * Date: 2021/5/12
 * Time: 21:19
 * Description: No Description
 */
public class MyLogoutSuccessHandler implements LogoutSuccessHandler {
    @Override
    public void onLogoutSuccess(HttpServletRequest req, HttpServletResponse resp, Authentication authentication) throws IOException, ServletException {
        // 1. 设置响应的数据格式
        resp.setContentType("application/json;charset=utf-8");
        // 2. 响应状态码
        resp.setStatus(HttpStatus.OK.value());
        // 3. 设置响应数据
        PrintWriter writer = resp.getWriter();
        Map<String,Object> resultMap = new HashMap<>();
        // 系统中的状态标识, 使用httpStatus 状态码很容易,产生 混淆 不方便调试错误
        resultMap.put("code",100012);
        resultMap.put("msg","success");
        // 把响应数据转成 json
        String resultJson = JSONUtil.toJsonStr(resultMap);
        writer.print(resultJson);
        writer.flush();
        writer.close();
    }
}
